import { Request, Response, json } from "express";
import { NextFunction } from "express-serve-static-core";
const express = require("express");
const router = express.Router();
import efetch from "../efetch";
const request = require("request");
const qs = require("querystring");
let rp = require("request-promise");
import jar from "../toughCookie";
import r from "../r";
import * as config from "../../config";

router.get("/hasAuth", async function(
  req: Request,
  res: Response,
  next: NextFunction
) {
  try {
    const newurl = `${config.api}/user/userinfo`;
    const ret = await r(
      {
        url: newurl
      },
      req,
      res
    );
    const jsonRet = JSON.parse(ret.body);
    if (jsonRet.code === 200) {
      jsonRet.data = true;
    } else {
      jsonRet.data = false;
    }
    res.json(jsonRet);
  } catch (e) {
    res.json({
      code: -1,
      data: undefined,
      msg: e
    });
  }
});

router.post("/logout", async function(
  req: Request,
  res: Response,
  next: NextFunction
) {
  const { code, username, password, verify } = req.body;
  if (process.env.NODE_ENV === "eh") {
    res.json({
      code: 200,
      data: undefined,
      msg: "success"
    });
  }
  try {
    const newurl = `${config.api}/user/logout`;

    const ret = await r(
      {
        url: newurl
      },
      req,
      res
    );
    const jsonRet = JSON.parse(ret.body);

    res.json(jsonRet);
  } catch (e) {
    res.json({
      code: -1,
      data: undefined,
      msg: e
    });
  }
});

router.get("/findUser", async function(
  req: Request,
  res: Response,
  next: NextFunction
) {
  // SESSION=YTM4YzRhZWItMzcxNS00NGFlLTkzNmQtNDUyY2M2MGViZjcw; path=/; domain=192.168.30.22; HttpOnly; Expires=Tue, 19 Jan 2038 03:14:07 GMT;
  const { code, username, password, verify } = req.body;
  try {
    const newurl = `${config.api}/user/findUser`;
    const ret = await r(
      {
        method: "GET",
        url: newurl
      },
      req,
      res
    );
    const jsonRet = JSON.parse(ret.body);
    res.json(jsonRet);
  } catch (e) {
    res.json({
      code: -1,
      data: undefined,
      msg: e
    });
  }
});

router.post("/login", async function(
  req: Request,
  res: Response,
  next: NextFunction
) {
  const { code, username, password, verify } = req.body;

  const newurl = `${config.api}/user/login`;
  try {
    const ret = await r(
      {
        url: newurl,
        body: JSON.stringify({
          code,
          password,
          username
        })
      },
      req,
      res
    );

    const jsonRet = JSON.parse(ret.body);
    res.json(jsonRet);
  } catch (e) {
    res.json({
      code: -1,
      data: undefined,
      msg: e
    });
  }
});
router.post("/enroll", async function(
  req: Request,
  res: Response,
  next: NextFunction
) {
  const { code, username, password, verify } = req.body;
  try {
    const newurl = `${config.api}/user/enroll`;
    const ret = await r(
      {
        url: newurl,
        body: JSON.stringify({
          code,
          password,
          username,
          verify
        })
      },
      req,
      res
    );
    const jsonRet = JSON.parse(ret.body);

    res.json(jsonRet);

    if (200 === jsonRet.code) {
      res.json(jsonRet);
    } else {
      res.json(jsonRet);
    }
  } catch (e) {
    res.json({
      code: -1,
      data: undefined,
      msg: e
    });
  }
});

router.get("/hasUser", async function(
  req: Request,
  res: Response,
  next: NextFunction
) {
  const { username } = req.query;

  try {
    const newurl = `${config.api}/user/findUser`;

    const ret = await r(
      {
        method: "GET",
        url: newurl
      },
      req,
      res
    );

    const jsonRet = JSON.parse(ret.body);

    if (jsonRet.code === 200) {
      res.json({
        code: 200,
        data: {},
        msg: "success"
      });
    } else {
      res.json({
        code: 403,
        data: undefined,
        msg: "has_not_user"
      });
    }
  } catch (e) {
    res.json({
      code: -1,
      data: undefined,
      msg: e
    });
  }
});

router.post("/savePwd", async function(
  req: Request,
  res: Response,
  next: NextFunction
) {
  const { username, password, code, verify } = req.body;
  try {
    const newurl = `${config.api}/user/resetPassword`;

    const ret = await r(
      {
        url: newurl,
        body: JSON.stringify({
          code,
          password,
          username
        })
      },
      req,
      res
    );
    const jsonRet = JSON.parse(ret.body);

    res.json(jsonRet);
  } catch (e) {
    res.json({
      code: -1,
      data: undefined,
      msg: e
    });
  }
});

export default router;
